<?php
require_once 'nOrm/TNOrmAdapter.inc';
/**
 *
 * @author kras
 * @orm.table type(class_name)
 * @orm.key ClassName
 * @orm.identity explicit
 *
 * @property array $AttrMeta
 * @property array $OrderOptions
 * @property string $ParentClass
 * @property string $Meta {@orm.scalar attr_meta}
 */
class TMSType extends TNOrmObject {
	const BASE_NONE = 'non';
	const BASE_GOODS = 'gds';
	const BASE_SERVICE = 'srv';
	const BASE_NOMENCLATURE = 'nom';
	const BASE_CLIENT = 'cli';

	/**
	 * @var string
	 * @orm.scalar class_name
	 */
	public $ClassName;

	/**
	 * @var string
	 * @orm.scalar name
	 */
	public $Name;

	/**
	 * @var string
	 * @orm.scalar base
	 */
	public $BaseClass;
	
	/**
	 * 
	 * @var string
	 * @orm.scalar article_class
	 */	
	public $ArticleClass;

	/**
	 * @var string
	 * @orm.scalar class_def
	 */
	public $ClassDefinition;
	
	/**
	 * @var string
	 * @orm.scalar order_options
	 */	
	public $Options;
	
	/**
	 * @var string
	 * @orm.scalar agreg_field
	 */
	
	public $AgregationAttribute;
	
	/**
	 * @var bool
	 * @orm.scalar forced
	 */
	public $IsForced;
	
	private $_meta_;
	
	protected $_orm_meta_;
	
	private $_order_options_;
	
	public function __set($nm,$value){
		switch ($nm){
			case 'Meta':{
				$this->_meta_ = false;
				parent::__set($nm,$value);
			}break;
			case 'AttrMeta':{
				if (is_array($value)){
					$this->_meta_ = $value;
					$this->Meta = json_encode($value);
				} else throw new TCoreException(TCoreException::ERR_BAD_VALUE);
			}break;
			case 'OrderOptions':{
				if (is_array($value)){
					$this->_order_options_ = $value;
					$this->Options = json_encode($value);
				} else throw new TCoreException(TCoreException::ERR_BAD_VALUE);
			}break;
			default:parent::__set($nm,$value);break;
		}
	}

	public function __get($nm){
		switch ($nm){
			case 'ParentClass':{
				switch ($this->BaseClass){
					case self::BASE_NONE:return 'TNOrmObject';break;
					case self::BASE_GOODS:return 'TMSGoods';break;
					case self::BASE_SERVICE:return 'TMSService';break;
					case self::BASE_NOMENCLATURE:return 'TMSNomenclature';break;
					case self::BASE_CLIENT:return 'TMSClient';break;
				}
				return 'TNOrmObject';
			}break;
			case 'AttrMeta':{
				if (!is_array($this->_meta_)){
					$result = $this->Meta?json_decode($this->Meta,true):array();
					$this->_meta_ = array();
					foreach ($result as $key=>$value)
						$this->_meta_[$key] = (object)$value;
				}
				return $this->_meta_;
			}break;
			case 'OrderOptions':{
				if (!is_array($this->_order_options_)){
					$result = $this->Options?json_decode($this->Options,true):array();
					$this->_order_options_ = array();
					foreach ($result as $key=>$value)
						$this->_order_options_[$key] = (object)$value;
				}
				return $this->_order_options_;
			}break;
			default:return parent::__get($nm);break;
		}
	}
	
	public function __toString(){
		return $this->Name;
	}
}